package com.lovo.csc.controller;

import com.alibaba.fastjson.JSON;
import com.lovo.commom.dto.VendorProductDto;
import com.lovo.commom.sysRemote.ISysSchemeRemote;
import com.lovo.csc.bean.SchemeBean;
import com.lovo.csc.bean.TenderBean;
import com.lovo.csc.service.ISchemeService;
import com.lovo.csc.service.ITenderService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("scheme")
public class SchemeController {
    @Autowired
    private ISchemeService schemeService;

    @Autowired
    private ITenderService iTenderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ISysSchemeRemote sysSchemeRemote;


    @RequestMapping("findcaigou")
    public Object findcaigou(Integer pageNo, int status) {
        return schemeService.shenhecaigouxinxi(pageNo, status);
    }

    @RequestMapping("findById")
    public Object findById(Long idNum) {
        return schemeService.findById(idNum);
    }

    @RequestMapping("updateById")
    public Object updateById(Long idNum, int status) {
        schemeService.updateById(idNum, status);
        //生成有主题和方案编号的新招标书
        SchemeBean scheme = schemeService.findById(idNum);
//        String title = scheme.getCause();
        String conte = scheme.getContent();
        iTenderService.addTenderBean(idNum, conte);
        //根据编号进行查询
        TenderBean t = iTenderService.findByIdNum(idNum);

        //把招标书对象转换成json格式
        String json = JSON.toJSONString(t);

        //向供应商发送采购方案
        VendorProductDto vendorProductDto = new VendorProductDto();
        vendorProductDto.setIdNum(idNum);
        vendorProductDto.setNum(scheme.getNum());
        vendorProductDto.setMoney(scheme.getMoney());

        String s = JSON.toJSONString(vendorProductDto);
//        System.err.println(s);
        //向供应商系统发送采购订单
        rabbitTemplate.convertAndSend("directCaiGouExchange","rabbit.CaiGou",s);
        //向供应商系统发送mq
        rabbitTemplate.convertAndSend("directRFpExchange", "rabbit.RFp",json );
        //远程调用修改后台采购状态
        String idNumStr = String.valueOf(idNum);
        sysSchemeRemote.updateIdNum(idNumStr,status,"or");
        return "ok";
    }


    @RequestMapping("updatetext")
    public Object updatetext(Long idNum, String text) {
        schemeService.updateById(idNum, 2);//不通过修改审核系统状态为不通过
        schemeService.updatetext(idNum, text);//添加不通过原因
        //远程调用修改采购不通过原因
        sysSchemeRemote.updateIdNum(String.valueOf(idNum),2,text);
        return "ok";
    }

}
